package com.uubx.controller;

import com.uubx.pojo.Department;
import com.uubx.pojo.Permission;
import com.uubx.pojo.Role;
import com.uubx.pojo.User;
import com.uubx.result.MessageCode;
import com.uubx.result.Result;
import com.uubx.result.ResultUtils;
import com.uubx.service.RoleService;
import com.uubx.service.UserService;
import com.uubx.vo.RoleVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Author wuguojun
 * @Date 2020/12/26 18:51
 * @Version 1.0
 */
@Controller
@ResponseBody
@RequestMapping("/role")
@Api(description ="角色相关的接口文档")
public class RoleController {

    @Autowired
    private RoleService roleService;


    /**
     * 分页查询所有的角色
     * @param page
     * @param limit
     * @return
     */

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation(value = "分页查询所有的角色",notes = "分页查询所有的角色",httpMethod = "GET",
            authorizations = {@Authorization(value = "token")})//请求需要token
    @ApiImplicitParams({@ApiImplicitParam(name = "page",value = "当前页数",required=true,paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "分页大小",required=true,paramType = "query")})
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result queryAllUserList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                       @RequestParam(value = "limit",defaultValue = "5")Integer limit){

        if (page==null||page.equals("")){
            return ResultUtils.buildFail(MessageCode.PAGE_IS_EMPTY);
        }
        if (limit==null||limit.equals("")){
            return ResultUtils.buildFail(MessageCode.LIMIT_IS_EMPTY);
        }
        List<Role> roles = roleService.queryAllRole((page - 1) * limit, limit);
        Long count = roleService.queryAllRoleCount();
        return ResultUtils.buildSuccess(roles,count);
    }

    /**
     * 通过角色名称查询
     * @param name
     * @return
     */

    @RequestMapping(value = "/queryByName",method = RequestMethod.GET)
    @ApiOperation(value = "通过角色名称查询",notes = "通过角色名称查询",httpMethod = "GET",
            authorizations = {@Authorization(value = "token")})//请求需要token
    @ApiImplicitParams({@ApiImplicitParam(name = "name",value = "角色名称",required=true,paramType = "query")})
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result queryByName(@RequestParam(value = "name")String name){
        if (name==null||name.equals("")){
            return ResultUtils.buildFail(MessageCode.NAME_EMPTY);
        }
        Role role = roleService.queryAllRoleByName(name);
        return ResultUtils.buildSuccess(role);
    }



    /**
     * 通过角色启用状态查询
     * @param status
     * @return
     */

    @RequestMapping(value = "/queryByStatus",method = RequestMethod.GET)
    @ApiOperation(value = "通过角色启用状态查询",notes = "通过角色启用状态查询",httpMethod = "GET",
            authorizations = {@Authorization(value = "token")})//请求需要token
    @ApiImplicitParams({@ApiImplicitParam(name = "status",value = "角色启用状态",required=true,paramType = "query")})
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result queryByStatus(@RequestParam(value = "status")Integer status){
        List<Role> roles = roleService.queryRoleByStatus(status);
        return ResultUtils.buildSuccess(roles);
    }


    /**
     * 通过角色id查询该角色下的所有账户
     * @param id
     * @return
     */
    @RequestMapping(value = "/queryAllUserByRoleId",method = RequestMethod.GET)
    @ApiOperation(value = "通过角色id查询该角色拥有的所有账户",notes = "通过角色id查询该角色拥有的所有账户",httpMethod = "GET",
            authorizations = {@Authorization(value = "token")})//请求需要token
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "角色id",required=true,paramType = "query")})
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result queryAllUserByRoleId(@RequestParam(value = "id")Integer id){
        List<User> users = roleService.queryAllUsersByRoleId(id);
        Long count = roleService.queryAllUsersByRoleIdToCount(id);
        return ResultUtils.buildSuccess(users,count);
    }


    /**
     * 查询所有的角色名称
     * @param
     * @return
     */
    @RequestMapping(value = "/queryAllRoleName",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有的角色名称",notes = "查询所有的角色名称",httpMethod = "GET",
            authorizations = {@Authorization(value = "token")})//请求需要token
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result queryAllRole(){
        List list = roleService.queryAllRoleName();
        return ResultUtils.buildSuccess(list);
    }


    /**
     * 添加角色
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "/addRole",method = RequestMethod.POST)
    @ApiOperation(value = "添加角色",notes = "添加角色",httpMethod = "POST",
            authorizations = {@Authorization(value = "token")})//请求需要token
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result addRole(@RequestBody RoleVo roleVo){

        Role role=new Role();
        role.setRoleName(roleVo.getRoleName());
        roleService.addRole(role);
        Role role1 = roleService.queryAllRoleByName(roleVo.getRoleName());

        Iterator<Integer> permission=roleVo.getPermissions().iterator();
        while(permission.hasNext()){
            roleService.addRolePermissionByRoleId(role1.getId(),permission.next());
        }
        return ResultUtils.buildSuccess();
    }


    /**
     * 编辑角色
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "/updateRole",method = RequestMethod.POST)
    @ApiOperation(value = "编辑角色",notes = "编辑角色",httpMethod = "POST",
            authorizations = {@Authorization(value = "token")})//请求需要token
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result updateRole(@RequestBody RoleVo roleVo){
        //删除角色所拥有的权限
        roleService.deleteAllPermissionByRoleId(roleVo.getRoleId());
        Iterator<Integer> permissionId=roleVo.getPermissions().iterator();
        while(permissionId.hasNext()){
            roleService.addRolePermissionByRoleId(roleVo.getRoleId(),permissionId.next());
        }
        return ResultUtils.buildSuccess();

    }

    /**
     * 通过角色id删除角色
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteRole",method = RequestMethod.GET)
    @ApiOperation(value = "通过角色id删除角色",notes = "通过角色id删除角色",httpMethod = "GET",
            authorizations = {@Authorization(value = "token")})//请求需要token
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "角色id",required=true,paramType = "query")})
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result deleteRole(@RequestParam(value = "id")Integer id){
        roleService.deleteRole(id);
        return ResultUtils.buildSuccess();
    }


    /**
     * 停用角色的状态
     * @param
     * @return
     */
    @RequestMapping(value = "/updateRoleStatus",method = RequestMethod.POST)
    @ApiOperation(value = "停用角色的状态",notes = "停用角色的状态",httpMethod = "POST")
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result updateUserByStatus(@RequestBody Role role){

        roleService.updateRoleByStatus(role);

        return ResultUtils.buildSuccess();
    }













}
